/**
 * 
 */
package com.shiplanet.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shiplanet.entity.Category;
import com.shiplanet.entity.Post;
import com.shiplanet.service.CategoryService;


/**
 * @author joesong
 *
 */
@Controller
public class CategoryAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Resource
	private CategoryService categoryService;

	// parameters for save()
	private Category category;
	
	private Integer parentId;

	private File[] photoFiles;
	
	public String save() throws Exception{
		if (parentId == null) {
			return "savecategorypage";
		}
		
		Category par = null;
		if (parentId != null) {
			par = categoryService.getCategory(parentId);
			if (par != null) {
				par.addChildCategory(category);
			}
		}

		String realRootPath = ServletActionContext.getServletContext()
				.getRealPath("/");	
		
		categoryService.saveCategoryWithPhoto(category, photoFiles, realRootPath);

		if (category.getId() != null) {
			category = null;
			parentId = null;
			photoFiles = null;
			return "savecategorypage";
		} else
			return ERROR;
	}

	
	// parameters for delete()
	private Integer categoryId;

	public String delete() {
		if (categoryId != null) {
			String realRootPath = ServletActionContext.getServletContext()
					.getRealPath("/");	
			
			Category category = categoryService.getCategory(categoryId);
			
			categoryService.deleteCategoryWithPhoto(category, realRootPath);
		}

		return "deletecategorypage";
	}
	
	// parameters for update()
	public String update() throws Exception{
		if (category == null && categoryId == null) {
			return "updatecategorypage1";
		} 
		
		if (category == null && categoryId != null) {
			category = categoryService.getCategory(categoryId);
			return "updatecategorypage2";
		} 

		if (category != null && categoryId != null) {
			
			String realRootPath = ServletActionContext.getServletContext()
					.getRealPath("/");
			Category originalCategory = categoryService.getCategory(categoryId);
	
			categoryService.updateCategoryWithPhoto(originalCategory, category, photoFiles, realRootPath);
			
			return "updatecategorypage1";
		}

		return "updatecategorypage1";
	}	
	
	// parameters for listing
	
	// categoryId
	private List<Post> postList;
	private List<Category> categoryList;
	
	public String list() {

		if (categoryId != null) {
			category = categoryService.getCategory(categoryId);
			postList = categoryService.findPostsByCategory(category);
		}
		
		categoryList = categoryService.findRootCategories();
		
		return "categorypage";
	}

	// parameters for searching
	
	// categoryId
	// category
	// postLists
	private String keyword;
	public String search() {
		if (categoryId != null) {
			category = categoryService.getCategory(categoryId);
		}
		postList = categoryService.findPostsByCategoryAndKeyword(category, keyword);	
		keyword=null;
		categoryId=null;
		
		categoryList = categoryService.findRootCategories();
		return "categorypage";
	}
	
	public Integer getCategoryId() {
		return categoryId;
	}

	public Integer getParentId() {
		return parentId;
	}

	public File[] getPhotoFiles() {
		return photoFiles;
	}

	public List<Post> getPostList() {
		return postList;
	}

	public Category getCategory() {
		return category;
	}

	public String getKeyword() {
		return keyword;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setPostList(List<Post> postList) {
		this.postList = postList;
	}

	public void setPhotoFiles(File[] photoFiles) {
		this.photoFiles = photoFiles;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

}
